//
//  afterSalesServiceViewController.m
//  uniform
//
//  Created by Apple on 15/8/30.
//  Copyright (c) 2015年 giuseppe. All rights reserved.
//

#import "afterSalesServiceViewController.h"
#import "addImageView.h"

@interface afterSalesServiceViewController ()<UITextViewDelegate,MBProgressHUDDelegate>{
    
    UITextView * _textview;
    UILabel * ReasonIslab;//反馈理由
    UIScrollView *bgView;
    addImageView *addImage;
     MBProgressHUD *HUD;
}

@end

@implementation afterSalesServiceViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    UIView *navView1 = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight)];
    navView1.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:navView1];
    
    //自定义的导航栏
    UIView *navView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, 64)];
    navView.backgroundColor = [UIColor whiteColor];
    navView.alpha = 0.90;
    
    UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
    backButton.frame = CGRectMake(10, 33, 27, 16);
    [backButton setBackgroundImage:[UIImage imageNamed:@"back"] forState:UIControlStateNormal];
    [backButton addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
    
    UILabel *titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(kScreenWidth/2-50, 20, 100, 40)];
    titleLabel.text = [NSString stringWithFormat:@"申请%@",self.topicString];
    titleLabel.textAlignment = NSTextAlignmentCenter;
    
    UIView * view = [[UIView alloc]initWithFrame:CGRectMake(0, 64, kScreenWidth, 1)];
    view.backgroundColor = UIColorWithRGBA(229, 229, 231, 1);
    
    
    [self initView];
    
    
    [self.view addSubview:view];
    [navView addSubview:backButton];
    [navView addSubview:titleLabel];
    [self.view addSubview:navView];
    
    
    
}

- (void)initView{
    
    bgView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 64, kScreenWidth, kScreenHeight-64)];
    bgView.contentSize = CGSizeMake(kScreenWidth, 667-64);
    [self.view addSubview:bgView];
    
    //点击主页收回事件
    UITapGestureRecognizer *tapGesture=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(event:)];
    [bgView addGestureRecognizer:tapGesture];

    
    UIView *headerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, 200)];
    headerView.backgroundColor = [UIColor whiteColor];
    

    UIImageView *goodImageView = [[UIImageView alloc]initWithFrame:CGRectMake(10, 20, 100, 180)];
    [goodImageView sd_setImageWithURL:[NSURL URLWithString:self.imageUrlString]];
    
    
    UILabel *nameLabel = [[UILabel alloc]initWithFrame:CGRectMake(120, 20, kScreenWidth-120, 30)];
    nameLabel.text = self.nameString;
    
    UILabel *orderNumberLabel = [[UILabel alloc]initWithFrame:CGRectMake(120, 50, kScreenWidth-120, 30)];
    orderNumberLabel.text = [NSString stringWithFormat:@"订单号:%@",self.orderNumberString];
    orderNumberLabel.textColor = [UIColor grayColor];
    orderNumberLabel.font = [UIFont systemFontOfSize:15];
    
    UILabel *sizeLabel = [[UILabel alloc]initWithFrame:CGRectMake(120, 80, 60, 30)];
    sizeLabel.text = self.sizeContentString;
    
    
    UILabel *priceLabel = [[UILabel alloc]initWithFrame:CGRectMake(120+60, 80, 60, 30)];
    priceLabel.text = [NSString stringWithFormat:@"￥:%@",self.priceString];
    priceLabel.textColor = mainColor;
    
    UILabel *amountLabel = [[UILabel alloc]initWithFrame:CGRectMake(120+60+60, 80, 60, 30)];
    amountLabel.text = [NSString stringWithFormat:@"X%@",self.amountString];
    
    
    UIView * view = [[UIView alloc]initWithFrame:CGRectMake(0, 200, kScreenWidth, 1)];
    view.backgroundColor = UIColorWithRGBA(229, 229, 231, 1);
    
    
    [headerView addSubview:goodImageView];
    [headerView addSubview:nameLabel];
    [headerView addSubview:sizeLabel];
    [headerView addSubview:orderNumberLabel];
    [headerView addSubview:priceLabel];
    [headerView addSubview:amountLabel];
    [headerView addSubview:view];

    
    [bgView addSubview:headerView];
    
    
    UILabel *titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(20, 200+10, 100, 30)];
    titleLabel.text = [NSString stringWithFormat:@"%@理由",self.topicString];
    
    [bgView addSubview:titleLabel];
    
    
    
    _textview = [[UITextView alloc] initWithFrame:CGRectMake(10,200+10+30, kScreenWidth-20, 80)];
    UIColor *customColor  = UIColorWithRGBA(218, 218, 218, 1);
    _textview.layer.borderColor = customColor.CGColor;
    _textview.layer.borderWidth = 1.0;
    _textview.font = [UIFont systemFontOfSize:15];
    //textview.keyboardType = UIKeyboardTypeDefault;
    _textview.delegate = self;
    [bgView addSubview:_textview];

    ReasonIslab = [[UILabel alloc]initWithFrame:CGRectMake(3, 10, kScreenWidth, 10)];
    ReasonIslab.text = [NSString stringWithFormat:@"请填写%@理由...",self.topicString];
    ReasonIslab.font = [UIFont systemFontOfSize:13];
    [_textview addSubview:ReasonIslab];
    
    addImage = [[addImageView alloc]initWithFrame:CGRectMake(0, _textview.frame.origin.y+_textview.frame.size.height+10, [UIScreen mainScreen].bounds.size.width, 95)];
    
    
    [bgView addSubview:addImage];

    UIButton *applyButton = [UIButton buttonWithType:UIButtonTypeCustom];
    applyButton.frame = CGRectMake(60, 667-60-64, kScreenWidth-120, 40);
    [applyButton setImage:[UIImage imageNamed:@"applyforReturn"] forState:UIControlStateNormal];
    [applyButton addTarget:self action:@selector(apply) forControlEvents:UIControlEventTouchUpInside];
    
    
    [bgView addSubview:applyButton];
}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    
    [bgView endEditing:YES];
    
}
- (void)event:(UITapGestureRecognizer *)gesture{
    
   [bgView endEditing:YES];
    
}


- (void) textViewDidChange:(UITextView *)textView{
    if ([textView.text length] == 0) {
        [ReasonIslab setHidden:NO];
    }else{
        [ReasonIslab setHidden:YES];
    }
}


- (void)apply{
    
    
    
    HUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    HUD.delegate = self;
    HUD.mode = MBProgressHUDModeIndeterminate;
    HUD.labelText = @"发送中";
    
    NSString *brief = _textview.text;
    
    //请求参数
    NSDictionary *paras= @{@"userId":@"1",
                           @"orderGoodsDetailId":self.suitidString,
                           @"contents":brief,
                           @"type":self.typeString,
                           @"orderId":self.orderNumberString
                           };
    
    NSLog(@"%@",paras);
    //调用请求
    [[myDataAccess shardWebUtil] orderCommentsWithURLString:kNetPath_Code_orderRefund  andParas:paras andTransferGottenData:^(id obj, NSError *error)  {
        
        NSLog(@"%@",obj);
        
        if (obj) {
            
            NSNumber *result = [obj objectForKey:@"resultCode"];
            
            if (result) {
                
                HUD.customView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"37x-Checkmark.png"]];
                HUD.mode = MBProgressHUDModeCustomView;
                HUD.labelText = @"发送成功";
                
                
                [self hudWasHidden:HUD];
                
                [self.navigationController popViewControllerAnimated:YES];
                
            }
            
            
        }else{
            
            //没数据
            
        }
        
    }];

 
    
 
    
}
- (void)back{
    
    [self.navigationController popViewControllerAnimated:YES];
    
}
#pragma mark  MBProgressHUD delegate
-(void)hudWasHidden:(MBProgressHUD *)hud
{
    [HUD removeFromSuperview];
    HUD = nil;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
